import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IO_1 {
    public static void main(String[] args) throws IOException {
        String url1 = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String url2 = "http://www.haoming8.cn/baobao/10881.html";
        String url3 = "http://www.haoming8.cn/baobao/7641.html";
        String get1 = webCrawler(url1);
        String get2 = webCrawler(url2);
        String get3 = webCrawler(url3);
        ArrayList<String> familyNameTempList = getData(get1, "(\\W{4})[，。]", 1);
        ArrayList<String> boyNameTempList = getData(get2, "([\\u4E00-\\u9FA5]{2})[、。]", 1);
        ArrayList<String> girlNameTempList = getData(get3, "(.. ){4}..", 0);

        ArrayList<String> familyNameList = new ArrayList<>();
        for (String str : familyNameTempList) {
            for (int i = 0; i < str.length(); i++) {
                familyNameList.add(str.charAt(i) + "");
            }
        }

        ArrayList<String> boyNameList = new ArrayList<>();
        for (String str : boyNameTempList) {
            if (!boyNameList.contains(str)) {
                boyNameList.add(str);
            }
        }

        ArrayList<String> girlNameList = new ArrayList<>();
        for (String str : girlNameTempList) {
            String[] strs = str.split(" ");
            for (String s : strs) {
                if (!girlNameList.contains(s)) {
                    girlNameList.add(s);
                }
            }
        }

        ArrayList<String> list = getInfos(familyNameList,boyNameList, girlNameList,50,50);
        Collections.shuffle(list);
        BufferedWriter bw = new BufferedWriter(new FileWriter("Java_11_17\\infos.txt"));
        for(String str: list) {
            bw.write(str);
            bw.newLine();
        }
        bw.close();
    }

    public static ArrayList<String> getInfos(ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {
        HashSet<String> boyhs = new HashSet<>();
        while (true) {
            if (boyhs.size() == boyCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyhs.add(familyNameList.get(0) + boyNameList.get(0));
        }
        HashSet<String> girlhs = new HashSet<>();
        while (true) {
            if (girlhs.size() == girlCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlhs.add(familyNameList.get(0) + girlNameList.get(0));
        }
        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        int age;
        for(String str : boyhs) {
            age = r.nextInt(10)+18;
            list.add(str + "-男-" + age);
        }
        for(String str : girlhs) {
            age = r.nextInt(10)+18;
            list.add(str + "-女-" + age);
        }
        return list;
    }

    public static ArrayList<String> getData(String str, String regex, int index) {
        ArrayList<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            list.add(matcher.group(index));
        }
        return list;
    }

    public static String webCrawler(String net) throws IOException {
        StringBuilder sb = new StringBuilder();
        URL url = new URL(net);
        URLConnection conn = url.openConnection();
        InputStreamReader isr = new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"));
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);

        }
        isr.close();
        return sb.toString();
    }
}
